<?php
require_once 'util/class.dbservice.php';

class FavoriteDao {

	public function addFavorite($id, $userId, $noteId, $timestamp, $src=0) {
		try {
			$collection = $this->getCollection($noteId);
			if(!$collection)
				return;

			$collection->insert(array('_id'=>$id, 'uid'=>$userId, 'nid'=>$noteId,
					'ts'=>$timestamp, 'src'=>$src));
		} catch(Exception $e) {
			syslog(LOG_ERR, "FavoriteDao.addFavorite error, reason=".$e);
		}
	}
	
	public function getFavorite($userId, $noteId, $timestamp) {
		try {
			$collection = $this->getCollection($noteId);
			if(!$collection)
				return;
	
			return $collection->findOne(array('uid'=>$userId, 'nid'=>$noteId,'ts'=>$timestamp));
		} catch(Exception $e) {
			syslog(LOG_ERR, "FavoriteDao.getFavorite error, reason=".$e);
			return null;
		}
	}

	private function getCollection($noteId) {
		$db=DBService::getDbById('note', $noteId);
		$collectionName = DBService::getCollectionNameById('favorites', $noteId);
		return $db->$collectionName;
	}
}